#TODO: 热部署

function status() {
    ppid=$(cat ${targetResourcePid})
    point=$(date '+%Y-%m-%d %H:%M:%S')

    #echo "${barStar30}${barStar30}${barStar30}${barStar30}"
    #echo "" | awk '{printf "%-30s%-10s%-10s%-10s%-10s%-30s%-60s\n","point","user","pid","%cpu","%mem","start","cmd"}' | sed -nr '/^$/!p'
    #echo "${barStar30}${barStar30}${barStar30}${barStar30}"
    #ps --no-headers -o user,pid,%cpu,%mem,stime,cmd --pid ${ppid} | awk -v point="${point}" '{printf "%-30s%-10s%-10s%-10s%-10s%-30s%-60s\n",point,$1,$2,$3,$4,$5,$6$7$8}' | sed -nr '/^$/!p'
    #ps --no-headers -o user,pid,%cpu,%mem,stime,cmd --ppid ${ppid} | awk -v point="${point}" '{printf "%-30s%-10s%-10s%-10s%-10s%-30s%-60s\n",point,$1,$2,$3,$4,$5,$6$7$8}' | sed -nr '/^$/!p'
    #echo "${barStar30}${barStar30}${barStar30}${barStar30}"


    logger "${barStar30}${barStar30}${barStar30}${barStar30}"
    #表头
    while read -r line; do logger "${line}"; done < <(echo "" | awk '{printf "%-30s%-10s%-10s%-10s%-10s%-30s%-60s\n","point","user","pid","%cpu","%mem","start","cmd"}' | sed -nr '/^$/!p')
    logger "${barStar30}${barStar30}${barStar30}${barStar30}"
    #表身
    while read -r line; do logger "${line}"; done < <(ps --no-headers -o user,pid,%cpu,%mem,stime,cmd --pid ${ppid} | awk -v point="${point}" '{printf "%-30s%-10s%-10s%-10s%-10s%-30s%-60s\n",point,$1,$2,$3,$4,$5,$6$7$8}' | sed -nr '/^$/!p')
    while read -r line; do logger "${line}"; done < <(ps --no-headers -o user,pid,%cpu,%mem,stime,cmd --ppid ${ppid} | awk -v point="${point}" '{printf "%-30s%-10s%-10s%-10s%-10s%-30s%-60s\n",point,$1,$2,$3,$4,$5,$6$7$8}' | sed -nr '/^$/!p')
    logger "${barStar30}${barStar30}${barStar30}${barStar30}"

    while read -r line; do logger "${line}"; done < <(${targetResourceExecEntry} -V 2>&1)
}


function terminate(){
    if [[ ! "${targetResourcePid}" ]]; then logger "Var [targetResourcePid] is not found."; return 1; fi
    if [[ ! "${targetResourceExecEntry}" ]]; then logger "Var [targetResourceExecEntry] is not found."; return 1; fi
    if [[ ! "${targetResourceExecConfig}" ]]; then logger "Var [targetResourceExecConfig] is not found."; return 1; fi
    #是否停止
    if [ ! -f ${targetResourcePid} ]; then logger "Already terminated."; return 0; fi
    #能否停止
    #停止
    local count=1
    local limit=10
    while [[ -f ${targetResourcePid} ]] && (( count <= 10 )); do
        #停止
        ${targetResourceExecEntry} -p ${targetResourceConfigPrefix} -c ${targetResourceExecConfig} -s stop
        sleep $(( count * 5 ))s
        (( count = count + 1 ))
    done
    if (( count > limit )); then logger "Failed to terminate."; return 1; fi
    logger "Successfully terminated."
    return 0
}

function startup(){
    if [[ ! "${targetResourcePid}" ]]; then logger "Var [targetResourcePid] is not found."; return 1; fi
    if [[ ! "${targetResourceExecEntry}" ]]; then logger "Var [targetResourceExecEntry] is not found."; return 1; fi
    #是否启动
    if [ -f ${targetResourcePid} ]; then logger "Already started."; return 0; fi
    #能否启动
    #启动
    ${targetResourceExecEntry} -p ${targetResourceConfigPrefix} -c ${targetResourceExecConfig}

    local count=1
    local limit=10
    while [[ ! -f ${targetResourcePid} ]] && (( count <= 10 )); do
        sleep $(( count * 5 ))s
        ppid=$(cat ${targetResourcePid})
        (( count = count + 1 ))
    done
    if (( count > limit )); then logger "Failed to start."; return 1; fi
    logger "Successfully started."
    return 0
}

function restart() {
    terminate
    if [[ ! $? == 0 ]]; then logger "Failed to terminate when restart."; return 1; fi
    startup
    if [[ ! $? == 0 ]]; then logger "Failed to start when restart."; return 1; fi
    return 0
}

function reload() {
    #重载配置
    ${targetResourceExecEntry} -p ${targetResourceConfigPrefix} -c ${targetResourceExecConfig} -s reload
    if (( $? > 0 )); then logger "Failed to reload."; return 1; fi
    logger "Successfully reloaded."
    return 0
}

function sshStatus () {
    set +e
    ssh ${user}@${host} "
    targetResource=${targetResource}
    targetResourcePid=${targetResourcePid}
    targetResourceExecEntry=${targetResourceExecEntry}
    targetResourceConfigPrefix=${targetResourceConfigPrefix}
    targetResourceExecConfig=${targetResourceExecConfig}
    barStar30=${barStar30}
    $(typeset -f checkIpAddress)
    $(typeset -f checkUser)
    $(typeset -f checkRevoker)
    $(typeset -f logger)
    $(typeset -f status)
    status
    "
    if [[ ! $? == 0 ]]; then return 1; logger "Ssh status failed." "error"; fi
    logger "Ssh status succeed."
    set -e
    return 0
}

function sshTerminate () {
    set +e
    ssh ${user}@${host} "
    targetResource=${targetResource}
    targetResourcePid=${targetResourcePid}
    targetResourceExecEntry=${targetResourceExecEntry}
    targetResourceConfigPrefix=${targetResourceConfigPrefix}
    targetResourceExecConfig=${targetResourceExecConfig}
    $(typeset -f checkIpAddress)
    $(typeset -f checkUser)
    $(typeset -f checkRevoker)
    $(typeset -f logger)
    $(typeset -f terminate)
    terminate
    "
    if [[ ! $? == 0 ]]; then return 1; logger "Ssh terminate failed." "error"; fi
    logger "Ssh terminate succeed."
    set -e
    return 0
}

function sshStartup () {
    set +e
    ssh ${user}@${host} "
    targetResource=${targetResource}
    targetResourcePid=${targetResourcePid}
    targetResourceExecEntry=${targetResourceExecEntry}
    targetResourceConfigPrefix=${targetResourceConfigPrefix}
    targetResourceExecConfig=${targetResourceExecConfig}
    $(typeset -f checkRevoker)
    $(typeset -f checkIpAddress)
    $(typeset -f checkUser)
    $(typeset -f logger)
    $(typeset -f startup)
    startup
    "
    if [[ ! $? == 0 ]]; then return 1; logger "Ssh start failed." "error"; fi
    logger "Ssh start succeed."
    set -e
    return 0
}

function sshRestart () {
    set +e
    ssh ${user}@${host} "
    targetResource=${targetResource}
    targetResourcePid=${targetResourcePid}
    targetResourceExecEntry=${targetResourceExecEntry}
    targetResourceConfigPrefix=${targetResourceConfigPrefix}
    targetResourceExecConfig=${targetResourceExecConfig}
    $(typeset -f checkRevoker)
    $(typeset -f checkIpAddress)
    $(typeset -f checkUser)
    $(typeset -f logger)
    $(typeset -f terminate)
    $(typeset -f startup)
    $(typeset -f restart)
    restart
    "
    if [[ ! $? == 0 ]]; then return 1; logger "Ssh restart failed." "error"; fi
    logger "Ssh restart succeed."
    set -e
    return 0
}

function sshReload () {
    set +e
    ssh ${user}@${host} "
    targetResource=${targetResource}
    targetResourcePid=${targetResourcePid}
    targetResourceExecEntry=${targetResourceExecEntry}
    targetResourceConfigPrefix=${targetResourceConfigPrefix}
    targetResourceExecConfig=${targetResourceExecConfig}
    $(typeset -f checkRevoker)
    $(typeset -f checkIpAddress)
    $(typeset -f checkUser)
    $(typeset -f logger)
    $(typeset -f reload)
    reload
    "
    if [[ ! $? == 0 ]]; then return 1; logger "Ssh reload failed." "error"; fi
    logger "Ssh reload succeed."
    set -e
    return 0
}